package com.edu.admin.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edu.admin.common.BaseService;
import com.edu.admin.entity.SysSchoolProduct;
import com.edu.admin.mapper.SchoolProductMapper;
import com.edu.admin.vo.SchoolProductVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 学校产品关联服务
 *
 * @author Wangjiong
 * 
 */
@Transactional
@Service
public class SchoolProductService extends BaseService<SchoolProductMapper, SysSchoolProduct> {
	
	/**
	 * 获取学校产品关联列表
	 * 
	 * @param schoolId
	 * @param productId
	 * 
	 * @return SysSchoolProduct
	 */
	public SysSchoolProduct getSchoolProductBySchoolIdAndProductId(Long schoolId,Long productId) {
		return this.mapper.getSchoolProductBySchoolIdAndProductId(schoolId, productId);
	}
	/**
	 * 获取学校产品关联列表
	 * 
	 * @param schoolId
	 * 
	 * @return List<SchoolProductVo>
	 */
	public List<SchoolProductVo> getSchoolProductListBySchoolId(Long schoolId) {
		return this.mapper.getSchoolProductListBySchoolId(schoolId);
	}
	
	/**
	 * 获取所有学校产品列表（包括停用学校产品）
	 * 
	 * @param schoolId
	 * @param productName
	 * @param useType
	 * 
	 * @return List<SchoolProductVo>
	 */
	public List<SchoolProductVo> getAllSchoolProductListBySchoolId(Long schoolId, String productName, Integer useType) {
		return this.mapper.getAllSchoolProductListBySchoolId(schoolId, productName, useType);
	}
	
	/**
	 * 获取所有学校产品列表（包括停用学校）（分页）
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @param schoolId
	 * @param productName
	 * @param useType
	 * 
	 * @return PageInfo<SchoolProductVo>
	 */
	public PageInfo<SchoolProductVo> getAllSchoolProductListPageBySchoolId(Integer pageNum, Integer pageSize, 
			Long schoolId, String productName, Integer useType){
		if (pageNum == null) {
			pageNum = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		PageHelper.startPage(pageNum, pageSize);
		
		List<SchoolProductVo> schoolProductList = this.mapper.getAllSchoolProductListBySchoolId(schoolId, productName, useType);
		
		PageInfo<SchoolProductVo> pageInfo = new PageInfo<SchoolProductVo>(schoolProductList);

		return pageInfo;
	}
	
	
	/**
	 * 删除学校产品关联
	 * 
	 * @param schoolId
	 * @return int
	 */
	public int deleteSchoolProductBySchoolId(Long schoolId) {
		return this.mapper.deleteSchoolProductBySchoolId(schoolId);
	}
	
	
	/**
	 * 学校产品关联保存
	 * 
	 * @param SysSchoolProduct
	 * 
	 * @return
	 */
	public void saveSchoolProduct(SysSchoolProduct schoolProduct) {

		Long schoolId = schoolProduct.getSchoolId();
		Long productId = schoolProduct.getProductId();
		
		SysSchoolProduct ssp = getSchoolProductBySchoolIdAndProductId(schoolId,productId);

		if (ssp == null) {
			
			insert(schoolProduct);

		} else {

			delete(ssp);
			insert(schoolProduct);

		}

		
	}


}
